#include "Engine.h"
#ifndef WIN32
#include <jni.h>
#include "SoundEngine.h"
#endif

Application* Application::application;

extern short iCurrentState;
extern char workingFolderName[50];
extern int s_frameCount;

///////////Sound   for porting
#ifndef WIN32
static jobject bridge;
static jmethodID android_playSound;
static jmethodID android_stopSound;
static JavaVM* jVM;
#endif
//////Sound

Application::Application():currentState(0)
{	
    application = this;		
}

Application::~Application()
{
}

Application* Application::GetApplication()
{
    return application;
}

void Application::Init()
{       
	Graphics::GetGraphics()->InitGraphics();
    glEnable(GL_DEPTH_TEST);
    glDepthFunc(GL_LESS);
    glEnable(GL_CULL_FACE);
}

void Application::Render()
{
    glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); //Black

    if (currentState)
    {
		currentState->Render();
		if ( VGPConfig::USE_DEBUG )
			TouchCore::GetTouch()->DebugTouch();	

		if ( VGPConfig::show_FPS )
			SceneEngine::GetSceneEngine()->PaintFPS();
    }
}

void Application::Update()
{
    if (currentState)
    {
		//update for touch
		TouchCore::GetTouch()->MenuUpdate();
		//update for state
        currentState->Update();
    }
	s_frameCount++;
}

void Application::Destroy()
{   
}

// Has mouse or pointer event
void Application::PointerEvent(int type, int x, int y)
{
	GameInput::GetGameInput()->PointerEvent(type, x, y);
}

void Application::KeyEvent(int type, int keyCode)
{
    GameInput::GetGameInput()->KeyEvent(type, keyCode);
}

void Application::SetState(short iCurrentState)
{
    if (currentState)
    {
		//reset touch before switch state
		TouchCore::GetTouch()->MenuReset();
		//destroy state
        currentState->Destroy();
    }
    currentState = NULL;

	//Init nextState
	GameState* state;
	switch(iCurrentState)
	{
	case GAMESTATE_LOGO:
		state = new LogoState();
		break;		
	}

	state->Init();
    currentState = state;
	s_frameCount = 0;
}

void Application::PauseGame()
{	
}

void Application::ResumeGame()
{	
}

///////////////////////////////For porting//////////////////////////

///////////Sound
// Play Sound
#ifndef WIN32
void SoundEngine::PlaySound(int id, int loop)
{
    JNIEnv *env;
    (jVM)->GetEnv((void**) &env, JNI_VERSION_1_6);
    (env)->CallVoidMethod(bridge, android_playSound, id, loop);
};

void SoundEngine::StopSound(int id)
{
    JNIEnv *env;
    (jVM)->GetEnv((void**) &env, JNI_VERSION_1_6);
    (env)->CallVoidMethod(bridge, android_stopSound, id);
}

void SoundEngine::StopAllSound()
{
    //for (int i = 0; i < MAX_SOUND; i++) //rem do
		//SoundEngine::StopSound(i);
}

extern "C"
{
	int JNI_OnLoad(JavaVM* vm, void* reserved)
	{
		JNIEnv *env;
		jVM = vm;

		LOGE("JNI ONLOAD");

		if((vm)->GetEnv((void**) &env, JNI_VERSION_1_6) != JNI_OK)
		{
			LOGE("Failed to get the environment using GetEnv()");
			return -1;
		}

		return JNI_VERSION_1_6;
	}
	
	void Java_game_com_GL2JNILib_init( JNIEnv* env, jobject thiz, jobject view)
	{
		
		//////////Sound
		bridge = view;		
		jclass glViewClass;
		
		(jVM)->GetEnv((void**) &env, JNI_VERSION_1_6);
		LOGE("New Global Ref");
		bridge = (jobject)(env)->NewGlobalRef( view);
		LOGE("Get Object Class");
		glViewClass = (env)->GetObjectClass( bridge);
		LOGE("hjgfdhgfhdsgfd    191");
		 
		android_playSound = (env)->GetMethodID(glViewClass, "playSound", "(II)V");
		android_stopSound = (env)->GetMethodID(glViewClass, "stopSound", "(I)V");
		
		
		//////////sound		
		Application::application = new Application();		
		Application::application->Init();			
		Application::application->SetState(GAMESTATE_LOGO);		
	}

	void Java_game_com_GL2JNILib_draw( JNIEnv* env, jobject thiz)
	{		
		Application::application->Update();		
		Application::application->Render();		
	}
	
	void Java_game_com_GL2JNILib_pointerEvent ( JNIEnv* env, jobject thiz, int state, int x, int y)
	{
		//LOGE ( "pointer Event: %d, %d, %d", state, x, y );
		Application::application->PointerEvent(state, x, y);
	}
	
	void Java_game_com_GL2JNILib_pause()
	{
		//Application::application->OnPauseGame();
	}
	
	void Java_game_com_GL2JNILib_resume()
	{
		//Application::application->OnResumeGame();
	}
}

#endif



